package com.wenhui.schoolyardinfo.mine.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wenhui.schoolyardinfo.R;
import com.wenhui.schoolyardinfo.callback.RcvOnItemViewClickListener;
import com.wenhui.schoolyardinfo.mine.bean.SelectTypeBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/7/5 15:04
 * introduce :
 */

public class SelectTypeAdapter extends RecyclerView.Adapter {

    private static final String TAG = "SelectTypeAdapter";
    private Context context;
    private List<SelectTypeBean> selectTypeBeanList = new ArrayList<>();
    private static final int HEADER_VIEW_TYPE = 1;
    private static final int CONTENT_VIEW_TYPE = 2;
    private static final int FOOTER_VIEW_TYPE = 3;

    public SelectTypeAdapter(Context context) {
        this.context = context;
    }

    public SelectTypeAdapter(Context context, List<SelectTypeBean> selectTypeBeanList) {
        this.context = context;
        this.selectTypeBeanList = selectTypeBeanList;
    }

    public void clearData() {
        selectTypeBeanList.clear();
        notifyDataSetChanged();
    }

    public void addAllData(List<SelectTypeBean> selectTypeBeanList) {
        this.selectTypeBeanList.addAll(selectTypeBeanList);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case HEADER_VIEW_TYPE:
                view = LayoutInflater.from(context).inflate(R.layout.item_select_photos_header, parent, false);
                return new HeaderHolder(view);
            case CONTENT_VIEW_TYPE:
                view = LayoutInflater.from(context).inflate(R.layout.item_select_photos_content, parent, false);
                return new ContentHolder(view);
            default:
                view = LayoutInflater.from(context).inflate(R.layout.item_select_photos_footer, parent, false);
                return new FooterHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeaderHolder) {
            HeaderHolder headerHolder = (HeaderHolder) holder;
            headerHolder.tevName.setText(selectTypeBeanList.get(position).getType());
            if(selectTypeBeanList.get(position).isChecked()){
                headerHolder.tevName.setTextColor(context.getResources().getColor(R.color.colorBlueA));
            }else {
                headerHolder.tevName.setTextColor(context.getResources().getColor(R.color.colorBlackA));
            }
        } else if (holder instanceof ContentHolder) {
            ContentHolder contentHolder = (ContentHolder) holder;
            contentHolder.tevName.setText(selectTypeBeanList.get(position).getType());
            if(selectTypeBeanList.get(position).isChecked()){
                contentHolder.tevName.setTextColor(context.getResources().getColor(R.color.colorBlueA));
            }else {
                contentHolder.tevName.setTextColor(context.getResources().getColor(R.color.colorBlackA));
            }
        } else {
            FooterHolder footerHolder = (FooterHolder) holder;
            footerHolder.tevName.setText(selectTypeBeanList.get(position).getType());
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == selectTypeBeanList.size() - 2) {
            return CONTENT_VIEW_TYPE;
        } else if (position == selectTypeBeanList.size() - 1) {
            return FOOTER_VIEW_TYPE;
        } else {
            return HEADER_VIEW_TYPE;
        }
    }

    @Override
    public int getItemCount() {
        return selectTypeBeanList.size();
    }

    protected class HeaderHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.tev_name)
        TextView tevName;
        @BindView(R.id.layout_name)
        RelativeLayout layoutName;

        private HeaderHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            layoutName.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.layout_name:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    tevName.setTextColor(context.getResources().getColor(R.color.colorBlueA));
                    break;
                default:
                    break;
            }
        }
    }

    protected class ContentHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.tev_name)
        TextView tevName;
        @BindView(R.id.layout_name)
        RelativeLayout layoutName;

        private ContentHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            layoutName.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.layout_name:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    tevName.setTextColor(context.getResources().getColor(R.color.colorBlueA));
                    break;
                default:
                    break;
            }
        }
    }

    protected class FooterHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.tev_name)
        TextView tevName;
        @BindView(R.id.layout_name)
        RelativeLayout layoutName;

        private FooterHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            layoutName.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.layout_name:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                default:
                    break;
            }
        }
    }

    private RcvOnItemViewClickListener rcvOnItemViewClickListener;

    public void setRcvOnItemViewClickListener(RcvOnItemViewClickListener rcvOnItemViewClickListener) {
        this.rcvOnItemViewClickListener = rcvOnItemViewClickListener;
    }
}
